package com.hubu.game.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hubu.game.entity.dto.UserDto;
import com.hubu.game.entity.pojo.User;
import com.hubu.game.service.IUserService;
import com.hubu.game.utils.Result;
import com.hubu.game.utils.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 * @author zjj
 * @since 2023-10-24
 */
@RestController
@Api("用户菜单")
@Slf4j
@RequestMapping("/game/user")
public class UserController {

    @Resource
    private IUserService userService;


    @GetMapping("userInfo")
    @ApiOperation("获取用户信息")
    public Result<UserDto> login(HttpServletRequest request) {

        //获取请求头中的token
        String accessToken = request.getHeader("accessToken");
        log.warn("token:{}", accessToken);

        User user = userService.getById(1);

        UserDto.UserDtoBuilder userDtoBuilder = UserDto.builder();
        ArrayList<String> permissions = new ArrayList<>();
        permissions.add("admin");
        UserDto userDto = userDtoBuilder.username(user.getUsername()).avatar("https://i.gtimg.cn/club/item/face/img/8/15918_100.gif")
                .permissions(permissions).userId(user.getUserId()).build();

        return Result.success(userDto);
    }

    @GetMapping("getUsername/{username}")
    @ApiOperation("根据用户名获取用户id")
    public Result<Integer> getUsername(@PathVariable String username) {

        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername, username);
        User user = userService.getOne(queryWrapper);
        if (user != null) {
            return Result.success(user.getUserId());
        }else{
            return Result.fail(ResultCode.USER_ACCOUNT_NOT_EXIST);
        }

    }
}
